package com.nny.alarm.more;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nny.alarm.R;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCore;
import com.uxwine.cmm.UxAdapter;
import com.uxwine.skin.Skin;

public class AlarmAdapter extends UxAdapter<AlarmDetailView>
{
	Context mCtx;
	LayoutInflater mInflater;
	TypedArray mStatesResId;
	TypedArray mColorResId;
	Typeface mTypeFace;
	
//	int[] mStates;
	
	public AlarmAdapter(Context ctx)
	{
		mCtx = ctx;
		mInflater = LayoutInflater.from(ctx);
		
		final Resources res = ctx.getResources();
		mStatesResId = res.obtainTypedArray(R.array.alarm_list_bkgnds);
		mColorResId = res.obtainTypedArray(R.array.alarm_list_states);		
		mData = new ArrayList<AlarmDetailView>();

		mTypeFace = Skin.getTypeface(ctx);
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		AlarmDetailView dv = (AlarmDetailView)getItem(position);
		dv.setResIds(mColorResId, mStatesResId);
		
		return dv.createView(mInflater, position, mTypeFace);
	}
	
	public void enableAlarm(int position, boolean enabled, boolean bNotify)
	{
		final AlarmDetailView dv = (AlarmDetailView)getItem(position);
		if(null == dv)
			return;
		final Alarm a = dv.mAlarm;
		if(dv.canEabled())
		{
			AlarmCore.enableAlarm(mCtx, a.id, enabled);			
			a.enabled = enabled;			
		}
		else
		{
			a.enabled = false;
		}
		if(bNotify)
			notifyDataSetChanged();
	}
	
	public void deleteAlarm(int position, boolean bNotify)
	{
		final AlarmDetailView dv = (AlarmDetailView)getItem(position);
		if(null == dv)
			return;
		final Alarm a = dv.mAlarm;
		AlarmCore.delAlarm(mCtx, a.id);
		remove(dv, bNotify);
	}
	
	public void recycle()
	{
		mColorResId.recycle();
		mStatesResId.recycle();
	}
}
